package cn.tedu.cwgl.controller;

import cn.tedu.cwgl.configs.MD5Util;
import cn.tedu.cwgl.entity.Privilege;
import cn.tedu.cwgl.entity.Role;
import cn.tedu.cwgl.entity.UserInfo;
import cn.tedu.cwgl.service.PrivilegeService;
import cn.tedu.cwgl.service.UserInfoService;
import cn.tedu.cwgl.util.*;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
public class UserInfoController {

    @Resource
    private UserInfoService userInfoService;
    @Resource
    private PrivilegeService privilegeService;


    @RequestMapping(value = {"/", "login.html"})
    public String toLogin(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        if (session.getAttribute(Config.CURRENT_USERNAME) == null) {
            return "login";
        } else {
            try {
                response.sendRedirect("/pages/index");
            } catch (IOException e) {
                e.printStackTrace();
                return "login";
            }
            return null;
        }

    }

    @RequestMapping(value = "/login.do")
    @ResponseBody
    public Result getUserInfo(UserInfo userInfo, HttpServletRequest request, HttpServletResponse response) {

//        if (!CaptchaUtil.ver(userInfo.getVerity(), request)) {
//            CaptchaUtil.clear(request);  // 清除session中的验证码
//            return ResultUtil.unSuccess("验证码错误");
//        }

        boolean userIsExisted = userInfoService.userIsExisted(userInfo);
        System.out.println(userIsExisted + " - " + request.getHeader("token"));
        UserInfo user = getUserInfo(userInfo);
        if ("client".equals(request.getHeader("token")) || !userIsExisted) {
            //用户不存在
            return ResultUtil.unSuccess("用户不存在");
        }
        if (userIsExisted && !user.getPassword().equals(MD5Util.md5(userInfo.getPassword()))) {
            return ResultUtil.unSuccess("用户名或密码错误！");
        } else {
            //将用户信息存入session
            userInfo = setSessionUserInfo(user, request.getSession());
            //将当前用户信息存入cookie
            setCookieUser(request, response);
            return ResultUtil.success("登录成功", userInfo);
        }
    }

    private UserInfo getUserInfo(UserInfo userInfo) {
        return userInfoService.getUserInfo(userInfo);
    }

    // 登录时将用户信息存到Cookie中
    private void setCookieUser(HttpServletRequest request, HttpServletResponse response) {
        UserInfo userInfo = getSessionUser(request.getSession());
        Cookie cookie = new Cookie(Config.CURRENT_USERNAME, userInfo.getUsername() + "_" + userInfo.getId());
        cookie.setMaxAge(60 * 60 * 24 * 7);   // 设置cookie的有效期为7天
        response.addCookie(cookie);
    }

    @RequestMapping("/getSessionUser")
    @ResponseBody
    private UserInfo getSessionUser(HttpSession session) {
        UserInfo userInfo = (UserInfo) session.getAttribute(Config.CURRENT_USERNAME);
        userInfo.setPassword(null);
        return userInfo;
    }

    // 通过用户信息获取用户权限，并将其存入到session中
    private UserInfo setSessionUserInfo(UserInfo userInfo, HttpSession session) {
        List<Privilege> privileges = privilegeService.getPrivilegeByRoleid(userInfo.getRoleid());
        userInfo.setPrivileges(privileges);
        session.setAttribute(Config.CURRENT_USERNAME, userInfo);
        return userInfo;
    }

    // 添加用户
    @RequestMapping("/user/add")
    @ResponseBody
    public Result addUser(UserInfo userInfo) {
        int result = userInfoService.add(userInfo);
        if (result > 0) {
            return ResultUtil.success();
        } else {
            return ResultUtil.unSuccess();
        }
    }

    // 修改用户
    @RequestMapping("/user/update")
    @ResponseBody
    public Result updateUser(UserInfo userInfo) {
        int result = userInfoService.update(userInfo);
        if (result > 0) {
            return ResultUtil.success();
        } else {
            return ResultUtil.unSuccess();
        }
    }

    @RequestMapping("/getAllRoles")
    @ResponseBody
    public Result<Role> getAllRoles() {
        List<Role> roles = userInfoService.getAllRoles();
        if (roles.size() > 0) {
            return ResultUtil.success(roles);
        } else {
            return ResultUtil.unSuccess();
        }
    }

    @RequestMapping("/getRoles")
    @ResponseBody
    public Result<Role> getRoles() {
        List<Role> roles = userInfoService.getRoles();
        if (roles.size() > 0) {
            return ResultUtil.success(roles);
        } else {
            return ResultUtil.unSuccess();
        }
    }

    @RequestMapping("/role/add")
    @ResponseBody
    public Result addRole(Role role) {
        int result = userInfoService.addRole(role);
        if (result > 0) {
            privilegeService.addDefaultPrivilegeWhenAddRole(role.getRoleid().toString());
            return ResultUtil.success();
        } else {
            return ResultUtil.unSuccess();
        }
    }


    @RequestMapping("/users/getUsersByWhere/{pageNo}/{pageSize}")
    public @ResponseBody
    Result getUsersByWhere(UserInfo userInfo, @PathVariable int pageNo, @PathVariable int pageSize, int role, HttpSession session) {
        if (1 == role) {
            userInfo.setHouseid("");
        }
        Utils.log(userInfo.toString());
        PageModel model = new PageModel<>(pageNo, userInfo);
        model.setPageSize(pageSize);
        return userInfoService.getUsersByWhere(model);
    }

    @RequestMapping("logout")
    public String logout(HttpServletRequest request, HttpServletResponse response) {
        try {
            delCookieUser(request, response);
            request.getSession().removeAttribute(Config.CURRENT_USERNAME);
        }catch (Exception e){
            return "login";
        }
        return "login";
    }

    @RequestMapping("/user/password/change")
    @ResponseBody
    public Result updateUserPassword(UserInfo userInfo, String password, String newPassword, String reNewPassword, HttpServletRequest request) {
        UserInfo user = getUserInfo(userInfo);
        if (!user.getPassword().equals(MD5Util.md5(password))) {
            return ResultUtil.unSuccess("原密码错误");
        } else if (!newPassword.equals(reNewPassword)) {
            return ResultUtil.unSuccess("确认密码错误");
        } else {
            userInfo.setPassword(MD5Util.md5(newPassword));
            System.out.println(MD5Util.md5(newPassword));
            int result = userInfoService.update(userInfo);
            if (result > 0) {
                return ResultUtil.success();
            } else {
                return ResultUtil.unSuccess();
            }

        }
    }

    private void delCookieUser(HttpServletRequest request, HttpServletResponse response) {
        UserInfo user = getSessionUser(request.getSession());
        Cookie cookie = new Cookie(Config.CURRENT_USERNAME, user.getUsername() + "_" + user.getId());
        cookie.setMaxAge(0);
        response.addCookie(cookie);
    }

    @RequestMapping("/user/del/{id}")
    @ResponseBody
    public Result deleteUser(UserInfo userInfo) {
        int result = userInfoService.delete(userInfo);
        if (result > 0) {
            return ResultUtil.success();
        } else {
            return ResultUtil.unSuccess();
        }
    }


    @RequestMapping("/register.html")
    public String toRegister(HttpServletRequest request, HttpServletResponse response) {
        return "register";
    }

    @RequestMapping("/register.do")
    @ResponseBody
    public Result register(UserInfo userInfo, HttpServletRequest request, HttpServletResponse response) {
        boolean userIsExisted = userInfoService.userIsExisted(userInfo);
        System.out.println(userIsExisted);
        if (userIsExisted) {
            return ResultUtil.unSuccess("用户名已经存在");
        }
        userInfo.setPassword(MD5Util.md5(userInfo.getPassword()));
        int result = userInfoService.add(userInfo);
        if (result > 0) {
            return ResultUtil.success();
        }
        return ResultUtil.unSuccess();
    }

}
